//
//  UserDetailInfoCell.m
//  QSBK
//
//  Created by Eric Wang on 5/7/15.
//  Copyright (c) 2015 Eric Wang. All rights reserved.
//
#import "UIImageView+WebCache.h"
#import "UserDetailInfoCell.h"

@implementation UserDetailInfoCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        [self addAllSubviews];
    }
    return self;

}

-(void)addAllSubviews
{
    _constraint = [[NSMutableArray alloc]init];
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.titleLabel];
    
    self.image = [[UIImageView alloc]init];
    self.image.hidden = YES;
    self.image.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.image];
    
    self.detailLabel = [[UILabel alloc]init];
    self.detailLabel.hidden = YES;
    self.detailLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.detailLabel];
    
    self.checkImage = [[UIImageView alloc]init];
    self.checkImage.image = [UIImage imageNamed:@"info_editable.png"];
    self.checkImage.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.checkImage];
   
    //titleLabel;
    [_constraint addObject:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeCenterY relatedBy: NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    [_constraint addObject:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:30]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(20)-[titleLabel(50)]-(>=0)-|" options:0 metrics:nil views:@{@"titleLabel":self.titleLabel}]];
    //checkImahe
    [_constraint addObject:[NSLayoutConstraint constraintWithItem:self.checkImage attribute:NSLayoutAttributeCenterY relatedBy: NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    [_constraint addObject:[NSLayoutConstraint constraintWithItem:self.checkImage attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:10]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[checkImage(10)]-(20)-|" options:0 metrics:nil views:@{@"checkImage":self.checkImage}]];
    //image
    [_constraint addObject:[NSLayoutConstraint constraintWithItem:self.image attribute:NSLayoutAttributeCenterY relatedBy: NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    [_constraint addObject:[NSLayoutConstraint constraintWithItem:self.image attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:30]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[image(30)]-(40)-|" options:0 metrics:nil views:@{@"image":self.image}]];
    //detailLabel
    [_constraint addObject:[NSLayoutConstraint constraintWithItem:self.detailLabel attribute:NSLayoutAttributeCenterY relatedBy: NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    [_constraint addObject:[NSLayoutConstraint constraintWithItem:self.detailLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:30]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[detailLabel(150)]-(40)-|" options:0 metrics:nil views:@{@"detailLabel":self.detailLabel}]];
    
    [self.contentView addConstraints:_constraint];
}
-(void)setInfoItem:(UserInfoItem *)infoItem
{
    _infoItem = infoItem;
    self.titleLabel.text = infoItem.itemTitle;
    if(infoItem.itemStyle==UserInfoItemImageStyle)
    {
        self.image.hidden = NO;
        [self.image sd_setImageWithURL:[NSURL URLWithString:infoItem.itemImage] placeholderImage:[UIImage imageNamed:@"default_users_avatar.png"]];
    }else if(infoItem.itemStyle == UserInfoItemTextStyle)
    {
        self.detailLabel.hidden = NO;
        self.detailLabel.text = infoItem.itemDetail;
    }else{
        
    }
}

@end
